package com.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dao.cargo.ContractDao;
import com.dao.cargo.ContractProductDao;
import com.dao.cargo.ExtCproductDao;
import com.domain.cargo.*;
import com.domain.vo.ContractProductVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.cargo.ContractProductService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ContractProductServiceImpl implements ContractProductService {

    @Autowired
    private ContractProductDao contractProductDao;

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private ExtCproductDao extCproductDao;

    @Override
    public PageInfo findAll(ContractProductExample example, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<ContractProduct> list = contractProductDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    //保存货物,同时也要增加合同的数量和金额
    public Integer save(ContractProduct contractProduct) {
        contractProduct.setId(UUID.randomUUID().toString());
        String contractId = contractProduct.getContractId();

        //1.计算更新后的金额
        Double amount = getAmount(contractProduct);
        contractProduct.setAmount(amount);

        Contract contract = contractDao.selectByPrimaryKey(contractId);//原来的合同对象
        //更新后的数量 = 原来的数量 + 新增货物的数量
        contract.setProNum(contract.getProNum() + contractProduct.getCnumber());
        //更新后的金额 = 原来的金额 + 新增货物的金额
        contract.setTotalAmount(contract.getTotalAmount() + amount);
        //更新合同
        contractDao.updateByPrimaryKeySelective(contract);
        //保存货物
        return contractProductDao.insertSelective(contractProduct);
    }

    @Override
    public Integer update(ContractProduct contractProduct) {
        String id = contractProduct.getId();
        String contractId = contractProduct.getContractId();

        //1.计算更新后的金额
        Double amount = getAmount(contractProduct);
        contractProduct.setAmount(amount);
        Contract contract = contractDao.selectByPrimaryKey(contractId);//原来的合同对象
        ContractProduct oldProduct = contractProductDao.selectByPrimaryKey(id);//原来的货物对象

        //3.更新后数量 = 原来合同数量-原来的获取数量+现在的货物数量
        contract.setProNum(contract.getProNum() - oldProduct.getCnumber() + contractProduct.getCnumber());
        //4.更新后金额 = 原来合同金额-原来金额+现在的金额
        contract.setTotalAmount(contract.getTotalAmount() - oldProduct.getAmount() + amount);

        //5.更新货物表和合同表
        contractDao.updateByPrimaryKeySelective(contract);
        return contractProductDao.updateByPrimaryKeySelective(contractProduct);
    }

    @Override
    //删除货物,应该将对应的附件清空,这里的id是货物id
    public Integer delete(String id) {
        //考虑附件
        //查询所有对应的附件
        ExtCproductExample extCproductExample = new ExtCproductExample();
        ExtCproductExample.Criteria criteria = extCproductExample.createCriteria();
        criteria.andContractProductIdEqualTo(id);
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(extCproductExample);
        //遍历删除所有的附件,那么此时需要同步更新数量和金额
        double extAmount = 0.0d;
        Integer extCount = 0;
        for (ExtCproduct extCproduct : extCproducts) {
            extCount += extCproduct.getCnumber();
            extAmount += extCproduct.getAmount();
            extCproductDao.deleteByPrimaryKey(extCproduct.getId());
        }

        //货物删除时,也要更新对应的合同数量和金额
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);//待删除的货物对象
        String contractId = contractProduct.getContractId();//合同id
        Contract contract = contractDao.selectByPrimaryKey(contractId);//合同对象
        //更新 = 原合同的数量和金额 - 货物的数量和金额
        contract.setProNum(contract.getProNum() - contractProduct.getCnumber());
        //修改后还应该附件数量 - 附加删除的数量
        contract.setExtNum(contract.getExtNum() - extCount);
        contract.setTotalAmount(contract.getTotalAmount() - contractProduct.getAmount() - extAmount);
        //更新合同
        contractDao.updateByPrimaryKeySelective(contract);
        //删除货物
        return contractProductDao.deleteByPrimaryKey(id);


    }

    @Override
    public ContractProduct findById(String id) {
        return contractProductDao.selectByPrimaryKey(id);
    }

    @Override
    public List<ContractProductVo> findByShipTime(String inputDate) {
        return contractProductDao.findByShipTime(inputDate);
    }

    @Override
    public void countMoneyByFactory() {
        List<CountMoney> countMoneys = contractProductDao.countMoneyByFactory();
        for (CountMoney countMoney : countMoneys) {
            countMoney.setDate(new Date());
            contractProductDao.insertCount(countMoney);
        }
    }

    private Double getAmount(ContractProduct contractProduct){
        Integer cnumber = contractProduct.getCnumber();
        Double price = contractProduct.getPrice();
        double amount = 0.0d;
        if (cnumber != null && price != null) {
            amount = cnumber * price;
        }
        return amount;
    }
}
